//
//  TerminateDelegate.m
//  cTypica
//
//  Created by David Kavanagh on 12/3/08.
//  Copyright 2008 directThought. All rights reserved.
//

#import "TerminateDelegate.h"

@implementation TerminateDelegate
@synthesize instanceInfo;

+ (id)delegate {
	TerminateDelegate *ret = [self alloc];
	return ret;
}

- (void) dealloc {
	[super dealloc];
}

// XML parser delegate methods
- (void)parser: (NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
	if ([elementName isEqualToString:@"instancesSet"]) {
		self.instanceInfo = [[NSMutableArray alloc] initWithCapacity:1];
	}
	else if ([elementName isEqualToString:@"item"]) {
		item = [TerminatingInstancesDescription alloc];
		inShutdown = NO;
		inPrevious = NO;
	}
	else if ([elementName isEqualToString:@"shutdownState"]) {
		inShutdown = YES;
	}
	else if ([elementName isEqualToString:@"previousState"]) {
		inPrevious = YES;
	}
}

- (void)parser: (NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
	NSString *cleanString = [currentStringValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
	[currentStringValue setString:@""];
	if ([elementName isEqualToString:@"instancesSet"]) {
	}
	else if ([elementName isEqualToString:@"item"]) {
		[self.instanceInfo addObject:item];
		[item release];
		item = nil;
	}
	else if ([elementName isEqualToString:@"instanceId"]) {
		item.instanceId = cleanString;
	}
	else if ([elementName isEqualToString:@"shutdownState"]) {
		inShutdown = NO;
	}
	else if ([elementName isEqualToString:@"previousState"]) {
		inPrevious = NO;
	}
	else if ([elementName isEqualToString:@"code"]) {
		if (inShutdown) {
			item.shutdownStateCode = [cleanString intValue];
		}
		else if (inPrevious) {
			item.previousStateCode = [cleanString intValue];
		}
	}
	else if ([elementName isEqualToString:@"name"]) {
		if (inShutdown) {
			item.shutdownState = cleanString;
		}
		else if (inPrevious) {
			item.previousState = cleanString;
		}
	}
}
@end
